package cn.tworice.client.web;

import cn.tworice.auth.util.LoginUserUtil;
import cn.tworice.log.annotation.Log;
import cn.tworice.client.service.ClassifyService;
import cn.tworice.common.entity.code.*;
import cn.tworice.common.util.excel.InductsListener;
import cn.tworice.common.vo.*;
import com.alibaba.excel.EasyExcel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;

import io.swagger.annotations.*;

import java.util.List;
import java.net.URLEncoder;
import java.util.ArrayList;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@RestController
@RequestMapping("/client/classify")
@CrossOrigin
@Log(table = "classify")
@Api(tags = "商品分类表")
public class ClassifyClient {
    @Autowired
    private ClassifyService service;

    @GetMapping("list")
    @ApiOperation("获取分页列表")
    public RequestResult list(@ApiParam("页码")
                              Integer page, @ApiParam("每页数量")
                              Integer pageSize, @ApiParam("查询条件")
                              Classify entity) {
        return RequestResult.success(201, "数据列表刷新成功")
                .appendData("list", service.list(page, pageSize, entity)).appendData("total", service.total(entity));
    }

    @PostMapping("add")
    @ApiOperation("更新数据")
    public RequestResult add(Classify entity) {
        entity.setCreator(LoginUserUtil.getLoginUserId());
        service.set(entity);
        return RequestResult.success("更新数据成功");
    }

    @DeleteMapping("del")
    @ApiOperation("删除数据")
    public RequestResult del(@ApiParam("数据ID数组")
                             Integer[] ids) {
        service.del(ids);
        return RequestResult.success("成功删除数据")
                ;
    }

    @PostMapping("inducts")
    @ApiOperation("Excel表格批量上传数据")
    public RequestResult inducts(@ApiParam("Excel文件")
                                 MultipartFile file) {
        try {
            InputStream inputStream = file.getInputStream();
            EasyExcel.read(inputStream, Classify.class, new InductsListener(service)).sheet().doRead();
            return RequestResult.success("批量导入数据成功")
                    ;
        } catch (Exception ex) {
            ex.printStackTrace();
            return RequestResult.error("批量导入数据失败")
                    ;
        }
    }

    /**
     * 导出模板     * @param response 结果     * @throws IOException 可能的IO异常
     */
    @GetMapping("template")
    @ApiOperation("导出Excel模板")
    public void template(HttpServletResponse response) throws IOException {
        response.setContentType("application/vnd.ms-excel")
        ;
        response.setCharacterEncoding("utf-8")
        ;
        String fileName = URLEncoder.encode("模板", "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx")
        ;
        EasyExcel.write(response.getOutputStream(), Classify.class).sheet("模板")
                .doWrite(new ArrayList<>());
    }

    @GetMapping("info")
    @ApiOperation("获取数据")
    public RequestResult info(Integer id) {
        return RequestResult.success(201).appendData("info", service.info(id));
    }
}